﻿//-----------------------------------------------------------------------
// <copyright file="Report.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <author>Mark Phillips</author>
// <email>v-mphill</email>
// <date>2004-08-25</date>
// <summary>Stores a time diff in the specified XML file.
// </summary>
//-----------------------------------------------------------------------

namespace Microsoft.Sdc.Tasks.Time
{
  using System;
  using System.Xml;
  using System.IO;
  using System.Globalization;
  using Microsoft.Build.Framework;

  /// <summary>
  /// Creates an XML element in an XML file to store a time and description.
  /// </summary>
  /// <remarks>
  /// <Time.Report XmlFile="output.xml" Description="A Description" Time="hh:mm:ss" Version="1.0.040825.000"/>
  /// <para>where:</para>
  /// <para><i>XmlFile (Required)</i></para>
  /// <para>The name of the XmlFile to which the report will be added.</para>
  /// <para><i>Description (Required)</i></para>
  /// <para>The description to give to the report.</para>
  /// <para><i>Time (Required)</i></para>
  /// <para>A valid time in the format hh:mm:ss.  This will be converted into seconds in the xml file.</para>
  /// <para><i>Group (Optional)</i></para>
  /// <para>A text value that can be used to group several times relating to one description.
  ///       This provides a way of tracking times for a description over several runs.</para>
  /// </remarks>
  /// <example>
  /// <code><![CDATA[
  /// <Project>
  ///     <Target Name="Test" >
  ///         <Time.Report XmlFile="output.xml" Description="A Description" Time="hh:mm:ss" Version="Some text"/>
  ///     </Target>
  /// </Project>
  /// ]]></code>    
  /// </example>
  public class Report : TaskBase
  {
    private string xmlFile = String.Empty;
    private string description = String.Empty;
    private string time = String.Empty;
    private string group = String.Empty;

    /// <summary>
    /// Initializes a new instance of the Diff class.
    /// </summary>
    public Report()
    {
    }

    /// <summary>
    /// Performs the action of this task.
    /// </summary>
    protected override void InternalExecute()
    {
      #region Execute code

      if (XmlFile == String.Empty || XmlFile.Length == 0)
      {
        throw new Exception("XmlFile must be specified");
      }

      if (Description == String.Empty || Description.Length == 0)
      {
        throw new Exception("Description must be specified");
      }

      if (Time == String.Empty || Time.Length == 0)
      {
        throw new Exception("Time must be specified");
      }

      DateTime validTime = DateTime.Parse(this.time);
      long seconds = (validTime.Hour * 3600) + (validTime.Minute * 60) + validTime.Second;

      XmlDocument xmlDoc = new XmlDocument();

      if (!System.IO.File.Exists(this.xmlFile))
      {
        xmlDoc.LoadXml("<TimingReport></TimingReport>");
      }
      else
      {
        xmlDoc.Load(this.xmlFile);
      }

      XmlElement timeElement = xmlDoc.CreateElement("Time");
      XmlAttribute descriptionAttribute = xmlDoc.CreateAttribute("Description");
      descriptionAttribute.Value = this.description;
      XmlAttribute timeAttribute = xmlDoc.CreateAttribute("Time");
      timeAttribute.Value = seconds.ToString();
      XmlAttribute groupAttribute = xmlDoc.CreateAttribute("Group");
      groupAttribute.Value = (this.Group == String.Empty ? "" : this.Group);

      timeElement.Attributes.Append(descriptionAttribute);
      timeElement.Attributes.Append(timeAttribute);
      timeElement.Attributes.Append(groupAttribute);

      xmlDoc.DocumentElement.AppendChild(timeElement);

      xmlDoc.Save(this.xmlFile);

      #endregion
    }

    [Required]
    public string XmlFile
    {
      get
      {
        return (xmlFile == null ? String.Empty : xmlFile);
      }
      set
      {
        xmlFile = value;
      }
    }

    [Required]
    public string Description
    {
      get
      {
        return (description == null ? String.Empty : description);
      }
      set
      {
        description = value;
      }
    }

    [Required]
    public string Time
    {
      get
      {
        return (time == null ? String.Empty : time);
      }
      set
      {
        time = value;
      }
    }

    public string Group
    {
      get
      {
        return (group == null ? String.Empty : group);
      }
      set
      {
        group = value;
      }
    }

  }
}
